package com.wang.store.controller;

import com.wang.store.service.ICartService;
import com.wang.store.util.JsonResult;
import com.wang.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("carts")
public class CartController  extends BaseController{
    @Autowired
    private ICartService iCartService;

    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCart(Integer uid, Integer pid, Integer amount, HttpSession session){

    iCartService.addToCart(getuidFromSession(session),
                            pid,amount,
                            getUsernameFromSession(session));
    return new JsonResult<>(OK);
    }

    @RequestMapping({"","/"})
    public JsonResult<List<CartVO>> getVOByUids(HttpSession session){
        List<CartVO> data = iCartService.getVOByUid(getuidFromSession(session));
        return new JsonResult<>(OK,data);
    }
    @RequestMapping({"{cid/num/add}"})
    public JsonResult<Integer> addNum(Integer cid, HttpSession session){
        Integer data = iCartService.addNum(cid, getuidFromSession(session), getUsernameFromSession(session));
        return new JsonResult<Integer>(OK,data);
    }

}
